﻿using Mjollnir.Live;
using Mjollnir.Live.Controls;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace NeWindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.SignOut();
        }

        LiveConnectSession session;

        private void button1_Click(object sender, EventArgs e)
        {
            this.SignIn();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.SignOut();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var value = this.progressBar1.Value;

            if (value == 100)
            {
                this.progressBar1.Value = 0;
            }
            else
            {
                this.progressBar1.Value = value + 1;
            }

        }

        void SignOut()
        {
            this.session = null;

            this.label1.Text = "";
            this.label2.Text = "";

            this.pictureBox1.BackColor = Color.Transparent;
            this.pictureBox1.Image = null;

            this.button1.Visible = true;
            this.button2.Visible = false;

            this.timer1.Stop();
            this.progressBar1.Value = 0;
            this.progressBar1.Visible = false;
        }

        void SignIn()
        {
            this.SignOut();

            this.button1.Visible = false;
            this.button2.Visible = false;

            using (var dialog = new SignInDialog())
            {
                // Update the ClientID with your app client Id that you created from https://account.live.com/developers/applications.
                //dialog.ClientId = "%YourAppClientId%";
                dialog.ClientId = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"..\Dropbox\Documents\Keys\PseudoLiveSDK_ClientID.txt")).First().Trim();
                //dialog.Locale = "ja";
                dialog.Scopes = new[] { "wl.signin" };

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.session = dialog.Session;
                }
                else
                {
                    if (dialog.Error == null)
                    {
                        MessageBox.Show("Cancelled.");
                    }
                    else
                    {
                        MessageBox.Show(dialog.Error.Message);
                    }

                    this.SignOut();
                    return;
                }
            }

            this.timer1.Start();
            this.progressBar1.Visible = true;

            var client = new LiveConnectClient(this.session);

            client.GetCompleted += this.ConnetClient_GetCompleted;
            client.GetAsync("me");
        }

        void ConnetClient_GetCompleted(object sender, LiveOperationCompltedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                this.label1.Text = (string)e.Result["name"];
                this.label2.Text = string.Join(", ", new[] { (string)e.Result["gender"], (string)e.Result["locale"] });

                this.pictureBox1.BackColor = Color.LightGray;

                var client = new LiveConnectClient(this.session);

                client.DownloadCompleted += this.ConnectClient_DownloadCompleted;
                client.DownloadAsync("me/picture");
            }
            else
            {
                if (e.Cancelled)
                {
                    MessageBox.Show("Cancelled.");
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                this.SignOut();
            }
        }

        void ConnectClient_DownloadCompleted(object sender, LiveDownloadCompltedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                try
                {
                    e.Result.Seek(0, SeekOrigin.Begin);

                    this.pictureBox1.Image = Image.FromStream(e.Result);
                }
                catch
                {
                    this.pictureBox1.BackColor = Color.Red;
                }
            }
            else
            {
                if (e.Cancelled)
                {
                    MessageBox.Show("Cancelled.");
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                this.SignOut();
            }

            this.button1.Visible = false;
            this.button2.Visible = true;

            this.progressBar1.Visible = false;
        }
    }
}
